package com.example.crystal.trippers.activity;

import android.app.Activity;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.crystal.trippers.entity.bottom_entity;

import com.example.crystal.trippers.R;
import com.example.crystal.trippers.adaper.ContentAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    //中间内容区域
    private ViewPager viewPager;

    //ViewPager适配器ContentAdapter
    private ContentAdapter adapter;

    private List<View> views;

    private bottom_entity bottomEntity = new bottom_entity();

    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化控件
        initView();

        //初始化底部按钮控件
        initEvent();
    }

    private void initEvent()
    {
        //设置按钮监听
        bottomEntity.l1.setOnClickListener(this);
        bottomEntity.l2.setOnClickListener(this);
        bottomEntity.l3.setOnClickListener(this);
        bottomEntity.l4.setOnClickListener(this);

        //设置滑动监听
        viewPager.setOnPageChangeListener(this);

    }

    private void initView(){

        //底部菜单4个LinearLayout
        this.bottomEntity.l1 = (LinearLayout) findViewById(R.id.l1);
        this.bottomEntity.l2 = (LinearLayout) findViewById(R.id.l2);
        this.bottomEntity.l3 = (LinearLayout) findViewById(R.id.l3);
        this.bottomEntity.l4 = (LinearLayout) findViewById(R.id.l4);

        //底部菜单4个ImageView
        this.bottomEntity.img1 = (ImageView) findViewById(R.id.img1);
        this.bottomEntity.img2 = (ImageView) findViewById(R.id.img2);
        this.bottomEntity.img3 = (ImageView) findViewById(R.id.img3);
        this.bottomEntity.img4 = (ImageView) findViewById(R.id.img4);

        //底部菜单的4个TextView
        this.bottomEntity.tv1 = (TextView) findViewById(R.id.tv1);
        this.bottomEntity.tv2 = (TextView) findViewById(R.id.tv2);
        this.bottomEntity.tv3 = (TextView) findViewById(R.id.tv3);
        this.bottomEntity.tv4 = (TextView) findViewById(R.id.tv4);

        //中间内容的ViewPager
        this.viewPager = (ViewPager) findViewById(R.id.vp_content);

        //适配器
        View page_01 = View.inflate(MainActivity.this, R.layout.page_01, null);
        View page_02 = View.inflate(MainActivity.this, R.layout.page_02, null);
        View page_03 = View.inflate(MainActivity.this, R.layout.page_03, null);
        View page_04 = View.inflate(MainActivity.this, R.layout.page_04, null);

        views = new ArrayList<View>();

        views.add(page_01);
        views.add(page_02);
        views.add(page_03);
        views.add(page_04);

        this.adapter = new ContentAdapter(views);
        viewPager.setAdapter(adapter);
    }


    //将底部的所有按钮复原，然后进行选择后着色
    private void restartButton()
    {
        //将图标设为默认值
        //默认是在第一项Nearby
        bottomEntity.img1.setImageResource(R.drawable.ic_menu_day);
        bottomEntity.img2.setImageResource(R.drawable.btn_star);
        bottomEntity.img3.setImageResource(R.drawable.ic_dialog_email);
        bottomEntity.img4.setImageResource(R.drawable.ic_dialog_map);

        //将textview设为灰色
        bottomEntity.tv1.setTextColor(Color.GRAY);
        bottomEntity.tv2.setTextColor(Color.GRAY);
        bottomEntity.tv3.setTextColor(Color.GRAY);
        bottomEntity.tv4.setTextColor(Color.GRAY);

    }



    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int arg0){
        restartButton();

        switch (arg0){
            case 0:
                bottomEntity.img1.setImageResource(R.drawable.ic_menu_send);
                bottomEntity.tv1.setTextColor(Color.BLACK);
                break;

            case 1:
                bottomEntity.img2.setImageResource(R.drawable.btn_star_big_on);
                bottomEntity.tv2.setTextColor(Color.BLACK);
                break;

            case 2:
                bottomEntity.img3.setImageResource(R.drawable.ic_menu_agenda);
                bottomEntity.tv3.setTextColor(Color.BLACK);
                break;

            case 3:
                bottomEntity.img4.setImageResource(R.drawable.ic_menu_compass);
                bottomEntity.tv4.setTextColor(Color.BLACK);
                break;

            default:
                break;
        }

    }

    @Override
    public void onClick(View view)
    {
        //每次点击后将所有的底部按钮的着色变为灰色，然后根据点击改变颜色
        restartButton();

        //着色的同时，页面进行跳转
        switch (view.getId()){
            case R.id.l1:
                bottomEntity.img1.setImageResource(R.drawable.ic_menu_send);
                bottomEntity.tv1.setTextColor(Color.BLACK);
                viewPager.setCurrentItem(0);
                break;

            case R.id.l2:
                bottomEntity.img2.setImageResource(R.drawable.btn_star_big_on);
                bottomEntity.tv2.setTextColor(Color.BLACK);
                viewPager.setCurrentItem(1);
                break;

            case R.id.l3:
                bottomEntity.img3.setImageResource(R.drawable.ic_menu_agenda);
                bottomEntity.tv3.setTextColor(Color.BLACK);
                viewPager.setCurrentItem(2);
                break;

            case R.id.l4:
                bottomEntity.img4.setImageResource(R.drawable.ic_menu_compass);
                bottomEntity.tv4.setTextColor(Color.BLACK);
                viewPager.setCurrentItem(3);
                break;

            default:
                break;
        }
    }
}
